package io.jpress.addon.securityinformation.controller;

import com.jfinal.aop.Inject;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import io.jboot.utils.StrUtil;
import io.jboot.web.controller.annotation.RequestMapping;
import io.jpress.addon.securityinformation.model.JpressAddonSecurityinformationSrd;
import io.jpress.addon.securityinformation.model.JpressAddonSecurityinformationSrdDeviceIdentification;
import io.jpress.addon.securityinformation.service.JpressAddonSecurityinformationSrdService;
import io.jpress.model.User;
import io.jpress.web.base.TemplateControllerBase;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;

/**
 * SecurityinformationSrdAPI
 * anjie 2020年3月24日
 */
@RequestMapping(value = "/securityInformationSrd")
public class SecurityInformationSrdController extends TemplateControllerBase {

    @Inject
    private JpressAddonSecurityinformationSrdService service;

    public void index(){

        User loginedUser = getLoginedUser();

        if(loginedUser==null){
            redirect("/user/login");
            return;
        }

//        //防止多线程获取相同的时间戳
//        synchronized (SecurityInformationSrdController.class) {
//            setAttr("OddNumbers", "MFDR" + System.currentTimeMillis());
//        }

        String language = get("language");
        if(language!=null && language.equals("ch")){
            render("srd_ch.html");
        }else{
            render("srd_en.html");
        }

    }


    private String getSysYear() {
        Calendar date = Calendar.getInstance();
        String year = String.valueOf(date.get(Calendar.YEAR));
        return year;
    }


    public void submit(){

        User loginedUser = getLoginedUser();

        if(loginedUser==null){
            renderJson(Ret.fail().set("msg", "请先登录(Please login)"));
            return;
        }

        JpressAddonSecurityinformationSrd srd =
                getModel(JpressAddonSecurityinformationSrd.class,
                        "srd");

        //----------------------------------------------------------------------------
        Record first = Db.findFirst("select max(report_no) as max from addon_s_s");
        String max = first.getStr("max");
        Integer num = 0000;
        if(max==null)
            num+=1;
        else
            num = Integer.valueOf(max.substring(max.length()-4, max.length()))+1;
        DecimalFormat df=new DecimalFormat("0000");
        String n=df.format(num);
        srd.setReportNo("MFDR"+getSysYear()+n);
        //----------------------------------------------------------------------------

        srd.setUserId(loginedUser.getId().intValue());
        StrUtil.escapeModel(srd);//防止xss漏洞
        String[] paraValues = getParaValues("srdds[]");
        ArrayList<JpressAddonSecurityinformationSrdDeviceIdentification> list = new ArrayList<>();
        for(String datas:paraValues){
            JpressAddonSecurityinformationSrdDeviceIdentification srddi  =
                    new JpressAddonSecurityinformationSrdDeviceIdentification();
            String[] split = datas.split(",",-1);
            srddi.setEquipment(StrUtil.escapeHtml(split[0]));
            srddi.setManufacturer(StrUtil.escapeHtml(split[1]));
            srddi.setModel(StrUtil.escapeHtml(split[2]));
            srddi.setSerialNumber(StrUtil.escapeHtml(split[3]));
            srddi.setTotalTime(StrUtil.escapeHtml(split[4]));
            srddi.setTotalCirculation(StrUtil.escapeHtml(split[5]));
            list.add(srddi);
        }
        service.saveSRD(srd,list);
        renderJson(Ret.ok());
    }

}
